package com.qf.air.search.mq;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.air.search.service.AirSearchService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

@Component
public class AirQueueListener {

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private AirSearchService airSearchService;

    @RabbitListener(queues = {"air_queue"})
    public void airQueueConsume(String msg, Channel channel, Message message) throws IOException {
        //1. 获取routingKey判断业务
        String routingKey = message.getMessageProperties().getReceivedRoutingKey();

        //2. 根据routingKey做具体业务操作
        switch (routingKey){
            case "air.add":
                //add： 将消息反序列化为map，调用service层的add方法， 成功后ack
                Map map = objectMapper.readValue(msg, Map.class);
                airSearchService.add(map);
                channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
                break;
            case "air.update":

                break;
            case "air.delete":

                break;
            default:
                // 记录，routingKey有问题！！！
        }



    }
}
